﻿Imports CodeDefinition
Imports System.Text
Imports System.IO

Module MainModule

    Private Const cERRORCODES_NoError As Int32 = 0
    Private Const cERRORCODES_Exception As Int32 = 1
    Private Const cERRORCODES_MISSING_ARGUMENT_NoArgument As Int32 = 2
    Private Const cERRORCODES_MISSING_ARGUMENT_WrongArgument As Int32 = 3
    Private Const cERRORCODES_MISSING_ARGUMENT_InvalidFormat As Int32 = 4

    Sub Main()

        Try
            Dim myCommandLineArgs As IList(Of String) = My.Application.CommandLineArgs

            Dim myCreateDemoData As Boolean = False
            Dim myOverwriteExistingFile As Boolean = False
            Dim myXMLFileName As String = Nothing

            If myCommandLineArgs.Count = 0 Then
                CloseApplication("You need to set the commandline argument file=[path to XML file]", cERRORCODES_MISSING_ARGUMENT_NoArgument)
            End If

            For Each myCmdLineArg As String In myCommandLineArgs
                Dim myKVP As String() = Split(myCmdLineArg, "=")
                Select Case myKVP.Length
                    Case 0
                        'no parameter given
                        CloseApplication("You need to set the commandline argument file=[path to XML file]", cERRORCODES_MISSING_ARGUMENT_NoArgument)
                    Case 1
                        Select Case myKVP(0).ToLower
                            Case "CreateDemoFile".ToLowerInvariant
                                myCreateDemoData = True
                            Case "OverwriteExistingFile".ToLowerInvariant
                                myOverwriteExistingFile = True
                            Case "h", "help"
                                CloseApplication(GetHelpText(), cERRORCODES_NoError)
                            Case Else
                                CloseApplication(String.Format("The following Argument was not set but is not allowed: '{0}'", myKVP(0)), cERRORCODES_MISSING_ARGUMENT_WrongArgument)
                        End Select
                    Case 2
                        Select Case myKVP(0).ToLowerInvariant
                            Case "File".ToLowerInvariant
                                myXMLFileName = myKVP(1)
                        End Select
                    Case Else
                        CloseApplication("The Argument was invalid format", cERRORCODES_MISSING_ARGUMENT_InvalidFormat)
                End Select
            Next

            'if no filename is given close the application 
            If (myXMLFileName Is Nothing) OrElse (myXMLFileName.Trim.Length = 0) Then CloseApplication("You need to set the commandline argument file=[path to XML file]", cERRORCODES_MISSING_ARGUMENT_NoArgument)

            'do it now :-)
            Dim myGeneratorFeedback As New GeneratorFeedback
            myGeneratorFeedback = CodeGenerator_DO.DoCreation(myXMLFileName, aGeneratorFeedback:=myGeneratorFeedback)

            Console.WriteLine(myGeneratorFeedback.ToString)

            'return no error
            CloseApplication("sucessfully", cERRORCODES_NoError)

        Catch ex As Exception
            CloseApplication(ex)
        End Try
        
    End Sub

    Private Function GetHelpText() As String
        Dim myMSG As New StringBuilder
        With myMSG
            .AppendLine("File={a full path to the XML file with the class definition}")
            .AppendLine("CreateDemoFile this switch will create a demo file")
            .AppendLine("OverwriteExistingFile this switch is only valid together with the CreateDeomFile switch and overwrites an existing XML File. By default the file will be not overwritten.")
            .AppendLine("h or help shows the help screen")
            .AppendLine("")
            .AppendLine("The commandline tool will have the following possible errorlevels:")
            .AppendLine("0  cERRORCODES_NoError                         no error")
            .AppendLine("1  cERRORCODES_Exception                       there was an not handled exception occured. Please inform the developer about this.")
            .AppendLine("2  cERRORCODES_MISSING_ARGUMENT_NoArgument     There was no argument set.")
            .AppendLine("3  cERRORCODES_MISSING_ARGUMENT_WrongArgument  wrong argument. There was an argument key used that is not supported")
            .AppendLine("4  cERRORCODES_MISSING_ARGUMENT_InvalidFormat  there was an error on the format. Typically there was an error like Name=value=value")
            .AppendLine("")
        End With
        Return myMSG.ToString
    End Function

    Private Sub CloseApplication(ByVal ex As Exception)
        Dim myMSG As New StringBuilder

        myMSG.Append(ex.Message)
        myMSG.AppendLine()
        If Not ex.InnerException Is Nothing Then
            myMSG.AppendLine("Inner exception:")
            myMSG.AppendLine(ex.InnerException.Message)
        End If

        CloseApplication(myMSG.ToString, cERRORCODES_Exception)
    End Sub

    Private Sub CloseApplication(ByVal aMessage As String, ByVal anErrorCode As Int32)
        Dim myMSG As New StringBuilder

        Select Case anErrorCode
            Case cERRORCODES_NoError
                myMSG.AppendLine("Done without any error")
            Case cERRORCODES_Exception
                myMSG.AppendLine(aMessage)
            Case cERRORCODES_MISSING_ARGUMENT_NoArgument, cERRORCODES_MISSING_ARGUMENT_WrongArgument,cERRORCODES_MISSING_ARGUMENT_InvalidFormat
                myMSG.AppendLine(aMessage)
                myMSG.AppendLine()
                myMSG.Append(GetHelpText())
            Case Else
                myMSG.AppendLine(aMessage)
        End Select
        myMSG.AppendLine()

        Console.Write(myMSG.ToString)

        Environment.Exit(anErrorCode)
    End Sub



End Module
